package com.ysxsoft.wdg.ui.activity.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.event.CommentEvent;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.widget.TimeCount;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ForgetPswActivity extends BaseActivity {


    private TimeCount timeCount;

    public static void start(Context context, int code) {
        Intent intent = new Intent(context, ForgetPswActivity.class);
        intent.putExtra("code", code);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    TextView btnCode;
    @BindView(R.id.et_psw1)
    EditText etPsw1;
    @BindView(R.id.et_psw2)
    EditText etPsw2;

    private int jumpCode = 0;
    private String mobile = "", code = "", pass = "", repass = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_psw;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);

        timeCount = new TimeCount(60000, 1000, btnCode);

        jumpCode = getIntent().getIntExtra("code", 0);
        titleContent.setText(jumpCode == 0 ? "忘记密码" : "修改登录密码");

    }

    @Override
    protected void setListener() {

    }


    @OnClick({R.id.title_finish, R.id.btn_code, R.id.btn_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.btn_code:
                mobile = etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(mobile)) {
                    ToastUtils.showToast("请输入手机号");
                    return;
                }

                if (mobile.length()!=11) {
                    ToastUtils.showToast("手机号码格式不正确");
                    return;
                }
                postCode();
                break;
            case R.id.btn_sure:
                mobile = etPhone.getText().toString().trim();
                code = etCode.getText().toString().trim();
                pass = etPsw1.getText().toString().trim();
                repass = etPsw2.getText().toString().trim();

                if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(code)
                        || TextUtils.isEmpty(pass) || TextUtils.isEmpty(repass)) {
                    ToastUtils.showToast("输入不能为空");
                    return;
                }

                mApiHelper.modifyPsw(mobile, code, pass, repass, new Observer<BaseBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        ToastUtils.showToast(baseBean.getMs());
                        if (baseBean.getCode().equals(CallbackCode.SUCCESS)){
                            EventBus.getDefault().post(new CommentEvent(CommentEvent.EventStatus.REGISTER_OK,mobile));
                            finish();
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
                break;
        }
    }

    private void postCode() {
        timeCount.start();
        mApiHelper.getMsmCode(mobile, new Observer<BaseBean<String>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseBean<String> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    code = baseBean.getData();
                } else {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
